package sklad;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * User: Vladimir
 * Date: 10.01.13 Time: 10:19
 */
public class RemainsGrabber {
    public static void main(String[] args) throws IOException, InterruptedException {
//        List<GoodItem> items = grabGlavpunkt();
        List<GoodItem> items = grabZaberiTovar();
        for (GoodItem item : items) {
            System.out.println(item);
        }
    }

    public static List<GoodItem> grabZaberiTovar() throws IOException {
        List<GoodItem> items = new ArrayList<GoodItem>();

        Connection.Response response = null;
        String cookieName = "PHPSESSID";
        response = Jsoup.connect("http://lc.zaberi-tovar.ru/index.php").
                data("user", "extamobi").
                data("pass", "extr@mobi").
                data("login", "extamobi").
                data("password", "extr@mobi").
                timeout(10000).
                method(Connection.Method.POST).execute();
        String cookieValue = response.cookies().get(cookieName);
        String url = "http://lc.zaberi-tovar.ru/data/controls/show_store.php";
        Document doc = Jsoup.connect(url).header("Cookie", cookieName + "=" + cookieValue).timeout(10000).get();

        Elements rows = doc.select("tr");
        for (Element row : rows) {
            Elements cells = row.select("td");
            if (cells.size() < 1) {
                continue;
            }
            GoodItem item = new GoodItem();
            item.article = cells.get(0).text();
            item.name = cells.get(1).text();
            item.count = Integer.parseInt(cells.get(2).text());
            items.add(item);
        }
        return items;
    }

    public static List<GoodItem> grabGlavpunkt() throws IOException {
        List<GoodItem> items = new ArrayList<GoodItem>();
        Connection.Response response = null;
        String cookieName = "PHPSESSID";
        response = Jsoup.connect("http://glavpunkt.ru/auth.html").ignoreHttpErrors(true).
                data("login", "extramobi").
                data("password", "extramobi821").
                timeout(10000).
                method(Connection.Method.POST).execute();
        String cookieValue = response.cookies().get(cookieName);

        String url = "http://glavpunkt.ru/sklad.html";
        Document doc = Jsoup.connect(url).header("Cookie", cookieName + "=" + cookieValue).timeout(10000).get();
        Elements rows = doc.select("table.goods-table tr");
        int i = 0;
        for (Element row : rows) {
            if (i == 0) {
                i++;
                continue;
            }
            Elements labels = row.select("td label");
            String article = labels.get(0).text();
            String name = labels.get(1).text();
            int count = Integer.parseInt(row.select("td span").get(0).text());
            System.out.println(article + " " + name + "\t" + count);
            i++;
            GoodItem item = new GoodItem();
            item.article = article;
            item.name = name;
            item.count = count;
            items.add(item);
        }
        return items;
    }
}
